package com.qingke.besuper.command.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.besuper.command.Command;
import com.qingke.besuper.command.UserCommand;
import com.qingke.besuper.pojo.User;
import com.qingke.besuper.util.BeSuperConsole;

public class ListQCommand extends UserCommand{
	private static Map<ListQuestionCode,Class<? extends Command>> modelMap = new HashMap<>();
	public static enum ListQuestionCode{
		ALL,
		MINE,
		OPEN,
		ID,
		HELP,
	}
	static {
		modelMap.put(ListQuestionCode.ALL,ListAllQuestionsCommand.class);
		modelMap.put(ListQuestionCode.MINE,ListMineQuestionsCommand.class);
		modelMap.put(ListQuestionCode.OPEN,ListOpenQuestionsCommand.class);
		modelMap.put(ListQuestionCode.ID,ListQuestionByQuestionIdCommand.class);
		modelMap.put(ListQuestionCode.HELP,HelpModelCommand.class);
	}
	public ListQCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		
		String model;
		while(true){
			model=BeSuperConsole.prompt("model:").trim().toUpperCase();
			ListQuestionCode lqCode=null;
			try{
				lqCode=ListQuestionCode.valueOf(model);
			}catch(Exception e){
				new ErrorCommand().execute();
				continue;
			}
			
			Class<?> modelCode=null;
			modelCode=modelMap.get(lqCode);
			if(modelCode!=null){
				try {
					Command command =(Command) modelCode.getConstructors()[0].newInstance(user);
					command.execute();
				} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
					e.printStackTrace();
				}
			}
			if(modelCode!=HelpModelCommand.class)
				break;
		}
	}

}
